/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef NETWORK_ADAPTER_H
#define NETWORK_ADAPTER_H

#include <mc/core/timer.h>
#include <mc/engine/service.h>

#include <device_tree/interface/NetworkAdapter.h>
#include <ncsi_over_mctp/ncsi_over_mctp_huawei.h>
#include <smbus/smbus.h>

namespace dev {

using ncsi_over_mctp_hw_ptr = std::shared_ptr<ncsi_over_mctp_huawei>;
using smbus_obj_ptr         = mc::shared_ptr<smbus>;

using os_reset_callback = std::function<void()>;

class NetworkAdapter : public mc::engine::interface<NetworkAdapter, gen::NetworkAdapter> {
public:
    NetworkAdapter()  = default;
    ~NetworkAdapter() {
        // 释放定时器资源，防止内存泄漏
        stop_ncsi_update_task();
        stop_smbus_update_task();
    }

public:
    // 实现基类的纯虚函数
    void HandleOsResetSignal() override;
    // ncsi over mctp 华为自定义命令实现
    bool update_firmware_version_by_ncsi();
    void start_ncsi_update_task_huawei(ncsi_over_mctp_hw_ptr ncsi_over_mctp,
                                       mc::milliseconds      interval);
    void stop_ncsi_update_task();
    void start_smbus_update_task(smbus_obj_ptr smbus_obj, mc::milliseconds interval);
    void stop_smbus_update_task();
    void update_firmware_version_by_smbus();
    
    void register_os_reset_callback(const os_reset_callback& callback);
    void unregister_os_reset_callback();

private:
    mc::core::timer*      m_ncsi_timer;
    mc::core::timer*      m_smbus_timer;
    ncsi_over_mctp_hw_ptr m_ncsi_over_mctp_huawei;
    smbus_obj_ptr         m_smbus_obj;
    
    // OS重置回调函数
    os_reset_callback m_os_reset_callback;
};

} // namespace dev

#endif // NETWORK_ADAPTER_H
